/*
 * Class performing operations on columns
 */
package core;

import java.io.Serializable;
import java.util.Observable;
import org.apache.log4j.Logger;
/**
 *
 * @author Sakis Vtdk
 */
public class TableColumn extends Observable implements Serializable, Cloneable
{
    public TableColumn()
    {
        this._dataType = null; 
        this._name = "";
        this._autoIncrement = false;
        this._primaryKey = false;
        this._defaultValue = "";

		this.addObserver(new ChangesObserver());
    }

	public TableColumn(String name, DataType dataType)
	{
		this._name = name;
        this._dataType = dataType;

        this._autoIncrement = false;
        this._primaryKey = false;
        this._defaultValue = "";

		this.addObserver(new ChangesObserver());
	}

	/**
	 * Creates a new column with the given name and the default data type (INTEGER)
	 * 
	 * @param name The name of the new column
	 */
	public TableColumn(String name)
	{
		this._name = name;
        this._dataType = DataType.INTEGER;

        this._autoIncrement = false;
        this._primaryKey = false;
        this._defaultValue = "";

		this.addObserver(new ChangesObserver());
	}

	@Override
	public TableColumn clone() throws CloneNotSupportedException
	{
		TableColumn tblCol = (TableColumn) super.clone();

		return tblCol;
	}
	
    
    // <editor-fold desc="Setters">
    
    public void setAutoIncrement(boolean _autoIncrement) {
        this._autoIncrement = _autoIncrement;
    }

    public void setId(int _id) {
        this._id = _id;
    }

    public void setName(String _name) {
        this._name = _name;
    }

    public void setPrimaryKey(boolean _primaryKey) {
        this._primaryKey = _primaryKey;
    }

    public void setDataType(String _type) 
	{
		//extract only the type, because it may be something like "VARCHAR(32)"
		_type = _type.split("\\(")[0];

		try
		{
			this.setDataType(DataType.valueOf(_type.toUpperCase()));
		}
		catch (IllegalArgumentException e)
		{
			log.error("The following error occured when setting the type of the column to '"+ _type +"': "+ e.getMessage());
		}
    }

	public void setDataType(DataType dataType)
	{
		this._dataType = dataType;

		//if this dataType accepts size, we set the default one
		if(this._dataType.hasSize())
		{
			this._size = this._defaultSize;
		}
	}
    
    public void setDefaultValue(String _defaultValue) {
        this._defaultValue = _defaultValue;
    }

	public void setSize(int size)
	{
		this._size = size;
	}

	public void setParentId(int parentId)
	{
		this._parentId = parentId;
	}
    
    // </editor-fold>
    
    // <editor-fold desc="Getters">
    
    public boolean isAutoIncrement() 
    {
        return _autoIncrement;
    }

    public int getId() 
    {
        return _id;
    }

    public String getName() {
        return _name;
    }

    public boolean isPrimaryKey() {
        return _primaryKey;
    }

    public String getDataType(boolean getSize) {
		String returnString = this._dataType.toString(); 

		if(getSize && this._dataType.hasSize())
		{
			returnString += "("+ this._size +")";
		}
		
        return returnString;
    }

    public String getDefaultValue() {
        return _defaultValue;
    }

	public int getSize()
	{
		return this._size;
	}

	public int getParentId()
	{
		return this._parentId;
	}
	
	public int getDefaultSize()
	{
		return this._defaultSize;
	}

    // </editor-fold>
    
	public enum DataType {
		TEXT (false, false, false),
		VARCHAR (true, false, false), 
		FLOAT (false, true, true), 
		INTEGER (false, true, true), 
		BIT (false, false, true);

		private DataType(boolean hasSize, boolean isAutoIncrementable, boolean isNumeric) 
		{
			this.hasSize = hasSize;
			this.isAutoIncrementable = isAutoIncrementable;
			this.isNumeric = isNumeric;
		}

		public boolean hasSize()
		{
			return this.hasSize;
		}

		public boolean isAutoIncrementable()
		{
			return this.isAutoIncrementable;
		}
		
		public boolean isNumeric()
		{
			return this.isNumeric;
		}

		private final boolean isAutoIncrementable;
		private final boolean hasSize;
		private final boolean isNumeric;
	}

    // <editor-fold desc="Private Variables">
    
	/**
	 * The id of the column
	 */
    private int _id;

	/**
	 * The id of the table this columns belongs to
	 */
	private int _parentId;

	/**
	 * The name of the column
	 */
    private String _name;

	/**
	 * The DataType of the column
	 */
	private DataType _dataType;

	/**
	 * The default value of the column (if any)
	 */
    private String _defaultValue;

	/**
	 * If the column is autoIncrement
	 */
    private boolean _autoIncrement;

	/**
	 * If the column is primary key
	 */
    private boolean _primaryKey;

	/**
	 * The size of the type of the column
	 */
	private int _size;

	/**
	 * The default size if the datatype accepts size
	 */
	private final int _defaultSize = 32;

	/**
	 * Logger instance
	 */
	private static Logger log = Logger.getLogger(TableColumn.class);
    
	/**
	 * The class' id, used for serialization of the object
	 */
	private static final long serialVersionUID = 3L;
    // </editor-fold>
}
